﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eloquera.Client;
using Model;

namespace _430GroupProject
{
    public class AdminServices
    {
        private static string dbErrorString = "Database Retrieval Error.";

        public static ErrorMessage createUser(String netID, String universityId, String email, String fullName, String password) {
            DB db = null;
            try
            {

                db = DataBase.getConnection();
                User newUser = new User(netID, universityId, email, fullName);
                db.Store(newUser);

                db.Close();
            }
            catch (Exception) {
                checkDB(db);
                ErrorMessage em = new ErrorMessage();
                em.Header = dbErrorString;
                em.Message = "Unable to create new user!";
                return em;
            }
            return null;
        }

        //public static void updateUser(String userID, String netID, String universityId, String email, String fullName)
        public static User updateUser(User updateUser)
        {
            DB db = null;
            User user = new User("d", "d", "d", "d");
            try
            {
                db = DataBase.getConnection();
                user = (User)db.ExecuteScalar("SELECT User WHERE NetID = " + "'" + updateUser.NetID + "'");
                user.UniversityID = updateUser.UniversityID;
                user.Email = updateUser.Email;
                user.FullName = updateUser.FullName;
                user.Permissions = updateUser.Permissions;
                user.WorksIn = updateUser.WorksIn;
                user.Manages = user.WorksIn;
                user.Alerts = updateUser.Alerts;

                db.Store(user);

                db.Close();
            }
            catch (Exception)
            {
                checkDB(db);
                ErrorMessage em = new ErrorMessage();
                em.Header = dbErrorString;
                em.Message = "Unable to update new user!";
                user.Error = em;
                return user;
            }

            return updateUser;
        }

        public static ErrorMessage updateUserPassword(String netID, String password) {
            DB db = null;
            try
            {
                db = DataBase.getConnection();
                User user = (User)db.ExecuteScalar("SELECT User WHERE NetID = " + "'" + netID + "'");
                user.Password = password;
                db.Store(user);
                db.Close();
            }
            catch (Exception)
            {
                checkDB(db);
                ErrorMessage em = new ErrorMessage();
                em.Header = dbErrorString;
                em.Message = "Unable to remove user from database.";
                return em;
            }
            return null;
        }

        public static ErrorMessage removeUser(String userID){
            DB db = null;
            try
            {
                db = DataBase.getConnection();
                var user = db.ExecuteScalar("SELECT User WHERE UserID = " + "'" + userID + "'");
                db.Delete(user);
                db.Close();
            }
            catch (Exception) {
                checkDB(db);
                ErrorMessage em = new ErrorMessage();
                em.Header = dbErrorString;
                em.Message = "Unable to remove user from database.";
                return em;
            }
            return null;
        }

        private static void checkDB(DB db)
        {
            if (db != null && db.IsOpen)
                db.Close();
        }
    }
}
